/*
 * LotQtyDiscrepencyNotificationJob.java
 *
 * Created on Jul 3, 2003, 02:45 AM
 */

package com.scalar.chiptrack.adaptor.jobs;

/**
 * @author Balaram Putta
 * @version
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.UserGroup;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.chiptrack.utils.StringUtils;


public class LotQtyDiscrepancyNotificationJob extends JobHandler
{

    public LotQtyDiscrepancyNotificationJob() { }
    
    public void execute() throws Exception 
    {
        LotQtyDiscrepancyRecordList lotList = null;
        ArrayList userList = new ArrayList();
        
        try
        {
            userList = UserDAO.loadUsersByGroupName( UserGroup.USER_GROUP_OPERATIONS, false /* Don't load expired and disabled users */ );
            if( userList.isEmpty() == true )
            {
                System.out.println( "There are zero users to notify... exiting" );
                return;
            }
            
            System.out.println( "number of users = " + userList.size() );
 
            lotList = InventoryLotDAO.loadAllLotQtyDiscrepancyRecords();
            
            if( ( lotList == null ) || ( lotList.size() == 0 ) ) 
            {
                System.out.println( "There are zero lots... exiting" );
                return;
            }
            
            System.out.println( "number of Lots that have discrepancy in Qty = " + lotList.size() );
            
            sendAlert( lotList, userList );
        }
        catch( Exception e )
        {
            System.err.println( "Exception while running LotQtyDiscrepencyNotificationJob: " + e );
            e.printStackTrace();
            throw e;
        }
    }
    
    private void sendAlert( ArrayList lotList, ArrayList userList ) throws Exception 
    {
        Iterator users = userList.iterator();
        User user = null;
        String email = null;
        StringBuffer emails = new StringBuffer();

        while( users.hasNext() ) 
        {
            user = (User) users.next();
            email = user.getEmail();

            if( email != null )
            {
                emails.append( email ).append( Mailer.EMAIL_SEPARATOR );
            }
        }

        String bcc = emails.toString();
        if( ( bcc == null ) || ( bcc.trim().length() == 0 ) ) 
        {
            return;
        }

        String from = ConfigManager.getSysAdminEmail();
        String to = ConfigManager.getSysAdminEmail();
        String subject = "Lot Quantity Discrepancy Notification, as of " +  StringUtils.formatDateToString( new Date( System.currentTimeMillis() ) );
        String message = createMessageBody( lotList );

        System.out.println( "FROM => " + from + "\nBCC => " + bcc );
        Mailer.send( from, to, null /*cc*/, bcc, subject, message, null /*fileAttachments*/, Mailer.CONTENT_TYPE_HTML );
    }


    private String createMessageBody( ArrayList lotList )
    {
        StringBuffer messageBody = new StringBuffer();
        Iterator lotListIterator = lotList.iterator();
        LotQtyDiscrepancyRecord lotQtyDiscrepancyRecord = null;
        int i=0;
        String bgColor = null;
        
        // HTML Page start
        messageBody.append( "<html><head>\n" );
        messageBody.append( "<style type=\"text/css\">" );
        messageBody.append( "#toc a{ font-family: Verdana;font-style : normal ;font-size : 9pt; font-weight :bold;}" );
        messageBody.append( "#toc p{font-family: Verdana;font-style : normal ;font-size : 9pt; font-weight :bold;text-align :center;color :#FFFFFF}" );
        messageBody.append( "#toc {border-style : solid;border-color :#000000 ;border-width :medium }" );
        messageBody.append( "</style></head><body bgcolor='FDF5E6'>" );

        // main table -- start
        messageBody.append( "<TABLE width='80%' align=center border=0><TBODY>" );
        
        // Heading -- start
        messageBody.append( "<TR><TD align=center bgColor=#993333 colSpan=2>" );
        messageBody.append( "<FONT color=#ffffff><FONT size=5><STRONG>Lot Quantity Discrepancy Report");
        messageBody.append( "</STRONG></FONT></FONT></TD></TR>" );
        // Heading -- end
        
        messageBody.append( "<TR><TD colSpan=2><BR></TD></TR>" );
        messageBody.append( "<TR><TD colSpan=2><BR>" );
        
        // Table - List of all lots - start
        messageBody.append( "<TABLE borderColor=#000000 cellSpacing=0 cellPadding=0 width='100%' border=1 bordercolor='#000000'><TBODY>" );
        messageBody.append( "<TR height=30 align=center>" );
        messageBody.append( "<TD align=center width='12%' bgColor=#993333><FONT color=#ffffff>Part<br>Number</FONT></TD>" );
        messageBody.append( "<TD align=center width='12%' bgColor=#993333><FONT color=#ffffff>Lot<br>Number</FONT></TD>" );
        messageBody.append( "<TD align=center width='12%' bgColor=#993333><FONT color=#ffffff>Process<br>Step</FONT></TD>" );
        messageBody.append( "<TD align=center width='26%' bgColor=#993333 colspan=2><FONT color=#ffffff>Previous<br>Qty In&nbsp;&nbsp;&nbsp;Date In</FONT></TD>" );
        messageBody.append( "<TD align=center width='26%' bgColor=#993333 colspan=2><FONT color=#ffffff>Current<br>Qty In&nbsp;&nbsp;&nbsp;Date In</FONT></TD>" );
        messageBody.append( "<TD align=center width='12%' bgColor=#993333><FONT color=#ffffff>Vendor</FONT></TD>" );
        messageBody.append( "</TR>" );
                      
        while( lotListIterator.hasNext() )
        {
            if( i%2 == 0 ) { bgColor = "#DEE1E2"; }
            else { bgColor = "#FFFFEE"; }

            lotQtyDiscrepancyRecord = ( LotQtyDiscrepancyRecord ) lotListIterator.next();
            
            messageBody.append( "<TR height=25 align=center>" );
            messageBody.append( "<TD align=center width='12%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getPartNumberForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=center width='12%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getLotNumberForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=center width='22%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getProcessStepNameForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=right  width='13%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getOldQtyForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=center width='13%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getOldQtyDateInForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=right  width='13%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getNewQtyForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=center width='13%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getNewQtyDateInForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "<TD align=center width='12%' bgColor=" ).append( bgColor ).append( " nowrap>&nbsp;" ).append( lotQtyDiscrepancyRecord.getVendorNameForHtmlView() ).append( "&nbsp;</td>" );
            messageBody.append( "</TR>" );

            i++;
        }

        messageBody.append( "</TBODY></TABLE></TD></TR></TBODY></TABLE><BR>" );
        // Table - List of all lots - end
            
        // Footer start
        messageBody.append ("<TABLE width='65%' border=0><TBODY>");
        messageBody.append ("<TR><TD>__________________________________________</TD></TR>");
        messageBody.append ("<TR><TD><FONT size=1>You are receiving this email because you have opted for it. <br>If you no longer wish");
        messageBody.append ("you receive this email. Please contact<br>the system administrator.</FONT></TD></TR>");
        messageBody.append ("</TBODY></TABLE><BR><BR></TD></TR></TBODY></TABLE>");
        // Footer end
            
        messageBody.append ("</BLOCKQUOTE></BODY></HTML>");
        // HTML Page end
        
        return messageBody.toString();
    }

    // test
    public static void main( String agrs[] ) throws Exception
    {
        System.setProperty( "cmd", "" );
        System.setProperty( "scalar.properties", "c:/chiptrack/config/scalar.properties" );
        new LotQtyDiscrepancyNotificationJob().execute();
    }
}